//
//  ZdbzpbVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/9.
//

import UIKit

class ZdbzpbVC: BaseVC {
    
    var scrollView: UIScrollView!
    
    var ybsl: LGFLoginImageTitleView!
    var lysl: LGFLoginImageTitleView!
    var yxts: LGFLoginImageTitleView!
    var ybgz: LGFLoginImageTitleView!
    var rygz: LGFLoginImageTitleView!
    
    var kbzyb: UILabel!
    var kbzyb_i: Int!
    var kbzry: UILabel!
    var kbzry_i: Int!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        getData()
    }
    
    func getData() {
        let paramet1: [String: Any] = [
            "pageSize": "9999",
            "page": "1",
            "roleIds": ["3124297098854400"],
            "hospitalId": EWEUser.lgf.hospitalId ?? "",
            "isDeleted": 1,
            "scope": "2",
        ]
        EWERequest.lgf("user", paramet1, .get) { [weak self] (data) in
            self?.kbzry.text = "可标注人员：" + ((data["totalItems"] as? Int) ?? 0).description
            self?.kbzry_i = (data["totalItems"] as? Int) ?? 0
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        let paramet2: [String: Any] = [
            "status": 20,
            "isDeleted": 1,
            "workflow":0
        ]
        EWERequest.lgf("sample/size", paramet2, .get) { [weak self] (data) in
            let num = (data as? String) ?? "0"
            self?.kbzyb.text = "可标注样本：" + num
            self?.kbzyb_i = Int(num)
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
    @objc func queding(_ sender: UIButton) {
        if ybsl.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写样本数量")
            return
        }
        if (Int(ybsl.textField.text!) ?? 0) > 30 {
            SVProgressHUD.showError("样本数量至多选择" + 30.description + "个")
            return
        }
        if (Int(ybsl.textField.text!) ?? 0) > kbzyb_i {
            SVProgressHUD.showError("样本数量至多选择" + kbzyb_i.description + "个")
            return
        }
        if lysl.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写人员数量")
            return
        }
        if (Int(lysl.textField.text!) ?? 0) != 2 {
            SVProgressHUD.showError("标注人员数量须为2个")
            return
        }
        if yxts.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写有效天数")
            return
        }
        if (Int(yxts.textField.text!) ?? 0) < 7 {
            SVProgressHUD.showError("有效天数最少7天")
            return
        }
        if ybgz.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择样本规则")
            return
        }
        if rygz.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择人员规则")
            return
        }
        let paramet: [String: String] = [
            "hospitalId" : EWEUser.lgf.hospitalId,
            "expiryDay" : yxts.textField.text ?? "",
            "sampleTotal" : ybsl.textField.text ?? "",
            "markEmpTotal" : lysl.textField.text ?? "",
            "sampleRule" : ybgz.lgf_Name ?? "",
            "userRule" : rygz.lgf_Name ?? "",
        ]
        EWERequest.lgf("label?rule=1", paramet, .post, JSONEncoding.default) { [weak self] (data) in
            SVProgressHUD.showSuccess("派包成功")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadBzrwbVC"), object: nil)
            Timer.lgf_After(S: 0.5) {
                self?.navigationController?.popViewController(animated: true)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }

}

